package com.ts.animal.service.impl;

import com.ts.animal.dao.InputsDao;
import com.ts.animal.dao.ParturitionRecordDao;
import com.ts.animal.domain.InputsDO;
import com.ts.animal.domain.ParturitionRecordDO;
import com.ts.animal.service.ParturitionRecordService;
import com.ts.common.utils.DataPerUitl;
import com.ts.common.utils.Result;
import com.ts.common.utils.ShiroUtils;
import com.ts.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ParturitionRecordServiceImpl implements ParturitionRecordService {
	@Autowired
	private ParturitionRecordDao parturitionRecordDao;
	@Autowired
	private InputsDao inputsDao;

	@Override
	public ParturitionRecordDO get(Integer id) {
		return parturitionRecordDao.get(id);
	}

	@Override
	public List<ParturitionRecordDO> list(Map<String, Object> map) {
		map.putAll(DataPerUitl.deptPer());
		return parturitionRecordDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		map.putAll(DataPerUitl.deptPer());
		return parturitionRecordDao.count(map);
	}

	@Override
	public int save(Result result, ParturitionRecordDO parturitionRecord) {
		int ret = 0;
		String idListStr = parturitionRecord.getRemark1();
		parturitionRecord.setRemark1(null);
		if (StringUtils.isNotEmpty(idListStr)) {
			String[] idList = idListStr.split(",");
			for (String inputId : idList) {
				InputsDO inputsDO = inputsDao.get(Integer.valueOf(inputId));
				if (inputsDO != null) {
					parturitionRecord.setBatchNum(inputsDO.getBatchNum());
					parturitionRecord.setDeptId(inputsDO.getDeptId());
					parturitionRecord.setNum(inputsDO.getNum());
					parturitionRecord.setTypeName(inputsDO.getTypeName());
					parturitionRecord.setVarietyName(inputsDO.getVarietyName());
					parturitionRecord.setInputsId(inputsDO.getId());
					parturitionRecord.setCreateTime(new Date());
					parturitionRecord.setCreateUser((int) ShiroUtils.getUserId().longValue());
					ret = +parturitionRecordDao.save(parturitionRecord);
				}
			}
		}

		if (ret == 0) {
			result.set(Result.FAIL, "保存失败", null);
		}
		return ret;


	}

	@Override
	public int update(Result result, ParturitionRecordDO parturitionRecord) {
		int ret = parturitionRecordDao.update(parturitionRecord);
		if (ret == 0) {
			result.set(Result.FAIL, "更新失败", null);
		}
		return ret;
	}

	@Override
	public int remove(Result result, Integer id) {
		int ret = parturitionRecordDao.remove(id);
		if (ret == 0) {
			result.set(Result.FAIL, "删除失败", null);
		}
		return ret;
	}

	@Override
	public int batchRemove(Result result, Integer[] ids) {
		int ret = parturitionRecordDao.batchRemove(ids);
		if (ret != ids.length) {
			result.set(Result.FAIL, "部分删除失败", null);
		}
		return ret;
	}

}
